package 牛客.剑指OFFER;


class TreeNode {
 int val = 0;
 TreeNode left = null;
 TreeNode right = null;

 public TreeNode(int val) {
 this.val = val;

 }

 }
public class 二叉树深度 {
    static int sum = 0;
    public static void main(String[] args){
        TreeNode treeROOT = new TreeNode(1);
        treeROOT.left = new TreeNode(2);
        treeROOT.right = new TreeNode(3);
        System.out.println(NodeSum(treeROOT));
    }
    public static int TreeDepth(TreeNode root) {//深度遍历
        if(root!=null){
            if(root.right!=null&&root.left!=null) return TreeDepth(root.left)>TreeDepth(root.right)?TreeDepth(root.left)+1:TreeDepth(root.right)+1;
            else if(root.right==null)  return TreeDepth(root.left)+1;
            else return TreeDepth(root.right)+1;
        }
        return 0;
    }


    public static int NodeSum(TreeNode root){
        if (root!=null){
            sum = sum+1;
            NodeSum(root.left);
            NodeSum(root.right);
        }
        return  sum;
    }

    public boolean IsBalanced_Solution(TreeNode root) {
        if (NodeSum(root) == NodeSum(root.left)+NodeSum(root.right)+1)
        {
            return  true;
        }
        if (IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right))return true;
        return false;
    }

}